/**
	镜像
 */
package zstack

import (
	"bc/util"
	"encoding/json"
	"fmt"
)

type image struct {

}
func (t *image)AddImage(body string,username string,password string,vir_type string, uid string) (string,error) {

	oauth,err := Oauth()
	if err != nil{
		return "",err
	}
	h := make(map[string]string)
	h["Content-Type"] = "application/json;charset=UTF-8"
	h["Authorization"] = "OAuth " + oauth
	h["X-Web-Hook"] = util.Config.Zstack.WebHook + "image_add_call_back?username="+username+"&password="+password+"&vir_type="+vir_type + "&uid=" + uid

	url := util.Config.Zstack.ApiUrl + "v1/images"

	result,err := util.Curl.Header(h).Body(body).Url(url).POST()
	return result,err

}
func (t *image)DelImage(uuid string,bsuuid string,del_mode string) (string,error) {

	oauth,err := Oauth()
	if err != nil {
		return "",err
	}
	header := make(map[string]string)
	header["Content-Type"] = "application/json;charset=UTF-8"
	header["Authorization"] = "OAuth " + oauth
	header["X-Web-Hook"] = util.Config.Zstack.WebHook + "image_del_call_back?uuid="+uuid

	url := util.Config.Zstack.ApiUrl + "v1/images/%s?backupStorageUuids=%s&deleteMode=%s"
	url = fmt.Sprintf(url,uuid,bsuuid,del_mode)

	result,err := util.Curl.Header(header).Url(url).DELETE()
	return result,nil

}
func (t *image)ExpungeImage(img_uuid string,body string)(string,error){

	oauth,err := Oauth()
	if err != nil {
		return "",err
	}
	header := make(map[string]string)
	header["Content-Type"] = "application/json;charset=UTF-8"
	header["Authorization"] = "OAuth " + oauth
	header["X-Web-Hook"] = util.Config.Zstack.WebHook + ""

	url := util.Config.Zstack.ApiUrl + "v1/images/%s/actions"
	url = fmt.Sprintf(url,img_uuid)

	result,err := util.Curl.Header(header).Body(body).Url(url).DELETE()
	return result,err

}
/**
	更新镜像信息
 */
func (t *image)UpdateImage(uuid string,body string,username string,password string,vir_type string) (string,error){

	oauth,err := Oauth()
	if err != nil {
		return "",err
	}
	header := make(map[string]string)
	header["Content-Type"] = "application/json;charset=UTF-8"
	header["Authorization"] = "OAuth " + oauth
	header["X-Web-Hook"] = util.Config.Zstack.WebHook + "image_update_call_back?username="+username+"&password="+password+"&vir_type="+vir_type

	url := util.Config.Zstack.ApiUrl + "v1/images/%s/actions"
	url = fmt.Sprintf(url,uuid)

	result,err := util.Curl.Header(header).Body(body).Url(url).PUT()
	return result,err

}
/**
	修改镜像状态
	status : disable enable
 */
func (t *image)ChangeImageState(uuid string,status string)(string,error){

	oauth,err := Oauth()
	if err != nil {
		return "",err
	}
	header := make(map[string]string)
	header["Content-Type"] = "application/json;charset=UTF-8"
	header["Authorization"] = "OAuth " + oauth
	header["X-Web-Hook"] = util.Config.Zstack.WebHook + ""

	url := util.Config.Zstack.ApiUrl + "v1/images/%s/actions"
	url = fmt.Sprintf(url,uuid)

	m := map[string]interface{}{}
	m["changeImageState"] = map[string]interface{}{
		"stateEvent":status,
	}
	byte,_ := json.Marshal(m)
	result,err := util.Curl.Header(header).Body(string(byte)).Url(url).PUT()
	return result,err

}

var Image = &image{}
